import { cloneDeep, debounce } from "lodash-es";

import scBall from "@/components/ball/sc-ball.vue";
import scOpening from "@/components/open/sc-opening.vue";
import sscBall from "@/components/ball/ssc-ball.vue";
import sscOpening from "@/components/open/ssc-opening.vue";
import pcddBall from "@/components/ball/pcdd-ball.vue";
import pcddOpening from "@/components/open/pcdd-opening.vue";
import lhcBall from "@/components/ball/lhc-ball.vue";
import ydhOpening from "@/components/open/ydh-opening.vue";
import ydhBall from "@/components/ball/ydh-ball.vue";
import scrollList from "@/components/scroll-list.vue";
import { reqGameHisrory } from "@/api/main";
import moment from "moment";

interface State {
  loading: boolean;
  finished: boolean;
  page: number;
  total_page: number;
  page_size: number;
  list: HistoryMo[];
}
export function useResult() {
  const gameDetail = inject("gameDetail") as ComputedRef<GameTotalMo>;
  const roomState = inject<RoomState>("roomState") as RoomState;

  const state = reactive<State>({
    loading: false,
    finished: false,
    total_page: 9999999,
    page_size: 20,
    page: 1,
    list: []
  });

  // 上期开奖号码
  const lotteryCode = computed(() => {
    return gameDetail.value?.LuckCode.split(",").map(n => Number(n)) || [];
  });

  // 号码总和
  const codeNumSum = computed(() => {
    let sum = 0;
    lotteryCode.value.forEach((item: number) => {
      sum += item;
    });
    return sum;
  });

  // 上期期号
  const preDrawIssue = computed(() => {
    return gameDetail.value?.LastNum.substr(-5) || "加载中";
  });

  // 展开历史开奖
  const switchResult = () => {
    roomState.resultOpen = !roomState.resultOpen;
  };

  // 获取历史开奖列表
  const getData = async () => {
    if (state.page > state.total_page) {
      state.finished = true;
      state.loading = false;
      return;
    }
    if (state.loading) return;
    try {
      state.loading = true;
      const res = await reqGameHisrory({
        StartDate: moment().format("YYYY-MM-DD 00:00:00"),
        EndDate: moment().format("YYYY-MM-DD 23:59:59"),
        LotteryId: roomState.gameId,
        PageSize: state.page_size,
        Page: state.page
      });
      // 加载更多时
      state.list = cloneDeep(res.data.List);
      state.loading = false;
      state.finished = true;
    } catch (error) {
      state.loading = false;
      state.finished = true;
    }
  };

  // 获取号码
  const getlotteryCode = (item: HistoryMo) => {
    return item.LuckCode.split(",").map(num => Number(num));
  };

  // 获取号码的和
  const getCodeNumSum = (item: HistoryMo) => {
    let sum = 0;
    getlotteryCode(item).forEach(item => {
      sum += Number(item);
    });
    return sum;
  };

  const refresh = debounce(async () => {
    if (roomState.resultOpen) {
      state.list = [];
      state.page = 1;
      state.total_page = 9999999;
      state.finished = false;
      await getData();
    }
  }, 1000);

  return {
    roomState,
    scBall,
    scOpening,
    sscBall,
    sscOpening,
    pcddBall,
    pcddOpening,
    lhcBall,
    ydhOpening,
    ydhBall,
    scrollList,
    state,
    lotteryCode,
    codeNumSum,
    preDrawIssue,
    switchResult,
    getData,
    refresh,
    getlotteryCode,
    getCodeNumSum
  };
}
